package com.dhcc.bpm.modules.system.controller;

import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.system.entity.Dept;
import com.dhcc.bpm.modules.system.service.IDeptService;
import com.dhcc.bpm.modules.system.vo.DeptVO;
import com.dhcc.bpm.modules.system.vo.DeptVO2;
import com.dhcc.bpm.modules.system.wrapper.DeptWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.node.INode;
import org.springblade.core.tool.utils.Func;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static org.springblade.core.cache.constant.CacheConstant.SYS_CACHE;
/**
 * @ClassName DeptController
 * @Description 控制器
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@RestController
@AllArgsConstructor
@RequestMapping( "/blade-system/dept")
@Api(value = "部门", tags = "部门")
@PreAuth("hasAnyRole('administrator', 'admin','test')")
public class DeptController extends BladeController {

	private IDeptService deptService;

	/**
	 * @Description 详情
	 * @Author sjm
	 * @Date 10:02 2020/2/22 0022
	 * @param dept
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.system.vo.DeptVO>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入dept")
    @ApiLog("部门详情")
	@Log(title = "部门详情", businessType = BusinessType.SELECT)
	public R<DeptVO> detail(Dept dept) {
		Dept detail = deptService.getOne(Condition.getQueryWrapper(dept));
		return R.data(DeptWrapper.build().entityVO(detail));
	}

	/**
	 * @Description 列表
	 * @Author sjm
	 * @Date 10:02 2020/2/22 0022
	 * @param dept
	 * @return org.springblade.core.tool.api.R<java.util.List<org.springblade.core.tool.node.INode>>
	 **/
	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "deptName", value = "部门名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "fullName", value = "部门全称", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "列表", notes = "传入dept")
    @ApiLog("部门列表")
	@Log(title = "部门列表", businessType = BusinessType.SELECT)
	public R<List<INode>> list(@ApiIgnore @RequestParam Map<String, Object> dept) {
		List<Dept> list = deptService.list(Condition.getQueryWrapper(dept, Dept.class).lambda().orderByAsc(Dept::getSort));
		return R.data(DeptWrapper.build().listNodeVO(list));
	}

	/**
	 * @Description 获取部门树形结构
	 * @Author sjm
	 * @Date 10:02 2020/2/22 0022
	 * @param tenantId
	 * @param bladeUser
	 * @return org.springblade.core.tool.api.R<java.util.List<com.dhcc.bpm.modules.system.vo.DeptVO>>
	 **/
	@GetMapping("/tree")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "树形结构", notes = "树形结构")
    @ApiLog("获取部门树形结构")
	@Log(title = "获取部门树形结构", businessType = BusinessType.SELECT)
	public R<List<DeptVO>> tree(String tenantId, BladeUser bladeUser) {
		List<DeptVO> tree = deptService.tree(Func.toStrWithEmpty(tenantId, bladeUser.getTenantId()));
		return R.data(tree);
	}

	/**
	 * @Description 新增或修改
	 * @Author sjm
	 * @Date 10:02 2020/2/22 0022
	 * @param dept
	 * @param user
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增或修改", notes = "传入dept")
	@CacheEvict(cacheNames = {SYS_CACHE}, allEntries = true)
    @ApiLog("新增或修改部门")
	@Log(title = "部门", businessType = BusinessType.INSERT)
	public R submit(@Valid @RequestBody Dept dept, BladeUser user) {
		if (Func.isEmpty(dept.getId())) {
			dept.setTenantId(user.getTenantId());
		}
		return R.status(deptService.submit(dept));
	}

	/**
	 * @Description 删除
	 * @Author sjm
	 * @Date 10:02 2020/2/22 0022
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "删除", notes = "传入ids")
	@CacheEvict(cacheNames = {SYS_CACHE}, allEntries = true)
    @ApiLog("删除部门")
	@Log(title = "部门", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(deptService.removeDept(ids));
	}



	@GetMapping("/queryAllDeptByUserId")
	@ApiOperation(value = "列表", notes = "传入dept")
	@ApiLog("部门列表")
	@Log(title = "部门列表", businessType = BusinessType.SELECT)
	public R<List<DeptVO2>> queryAllDeptByUserId() {
		BladeUser user = AuthUtil.getUser();
		List<DeptVO2> deptList=deptService.queryDeptByUserId();

		return R.data (deptList);
	}
}
